Skip to content

Honor db_constraint=False in migration schema editor#2226

Open
youdie006 wants to merge 1 commit into
tortoise:developfrom
youdie006:fix/migration-db-constraint
Open

Honor db_constraint=False in migration schema editor#2226
youdie006 wants to merge 1 commit into
tortoise:developfrom
youdie006:fix/migration-db-constraint

Conversation

@youdie006

Copy link
Copy Markdown

Description

The migration schema editor always emitted the FOREIGN KEY reference for ForeignKeyField / ManyToManyField, ignoring db_constraint=False — unlike the runtime schema generator, which respects it. grep db_constraint tortoise/migrations/ returned zero matches, confirming the migration subsystem ignored the flag.

Motivation and Context

Closes #2223. Models that set db_constraint=False (to avoid a hard DB-level FK while keeping the ORM relation) still got a constrained FK in generated migrations, defeating the flag.

How Has This Been Tested?

  • _get_fk_field_definition: guard the _get_fk_reference_string(...) append on fk_field.db_constraint (mirrors backends/base/schema_generator.py:264). Covers create_model and add_field.
  • _get_m2m_table_definition: a no-constraint through-table template selected when not field.db_constraint (mirrors schema_generator.py:328/370); the unique index is still emitted.
  • 3 new tests in tests/migrations/test_schema_editor_sql.py (FK in create_model, FK via add_field, M2M) assert no REFERENCES/FOREIGN KEY while the column remains — red→green. Full sqlite suite: 1912 passed, 0 failures.

Checklist

  • Tests added
  • CHANGELOG.rst updated
  • ruff check / ruff format --check clean

The migration schema editor always emitted the FOREIGN KEY reference for
ForeignKeyField/ManyToManyField regardless of db_constraint, unlike the runtime
schema generator. Guard FK emission in _get_fk_field_definition on
fk_field.db_constraint, and add a no-constraint M2M through-table template,
mirroring schema_generator. The plain column (and through table) are still
emitted; only the FK constraint is dropped when db_constraint=False.
@codspeed-hq

codspeed-hq Bot commented Jun 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing youdie006:fix/migration-db-constraint (53d9596) with develop (bc4f971)

Open in CodSpeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regarding db_constraint, I think this is a bug

1 participant